package emotionIndex;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class InvertedIndexMapper extends Mapper<Object, Text, Text, Text>{
	
	Text v = new Text();
	static ArrayList<String> exceptWord = new ArrayList<String>();
	static{
		String tempExcept[] = {"a", "an", "and", "are", "as", "at", "be", "but", 
				"by","for", "if", "in", "into", "is", "it","no", "not", "of", "on", 
				"or", "such","that", "the", "their", "then", "there", "these","they", 
				"this", "to", "was", "will", "with", "about"};
		for (String w : tempExcept)
			exceptWord.add(w);
	}
	
	//for each pattern
	public void map(Object key, Text value, Context context)
		throws IOException, InterruptedException {
		String line = value.toString();
		if (line.startsWith("#"))
			return;
		String info[] = line.split("\t");
		String pos = info[0];
		String posScore = info[2];
		String negScore = info[3];
		if (posScore.equals("0") && negScore.equals("0")) return;
		String terms[] = info[4].split(" ");
		for (int i = 0; i < terms.length; i ++){
			String term = terms[i].substring(0, terms[i].indexOf("#"));
			if (!term.matches("[a-zA-Z_]+"))
				return;
			context.write(new Text(term), new Text(pos + "#" + posScore + "#" + negScore));
		}
	}
}
